# used libraries
if (!require("ggplot2")) install.packages("ggplot2")
if (!require("plotly")) install.packages("plotly")
# R package DT provides an R interface to the JavaScript library DataTables.
if (!require("DT")) install.packages("DT")
# geom_mark_hull
if (!require("concaveman")) install.packages("concaveman")
if (!require("ggforce")) install.packages("ggforce")

plotly - Documentation

This document is essentially a brief notes of the book above.

Motivation

library(ggplot2)
mpg

Exploratory Data Analysis with Static plots

library(ggplot2)
ggplot(mpg, aes(displ, hwy)) + geom_point()

What are max hwy? ’Outliers`?

library(plotly)
p <- ggplot(mpg, aes(displ, hwy)) + geom_point()
ggplotly(p)

What are those models:

library(plotly)
m <- highlight_key(mpg)
p <- ggplot(m, aes(displ, hwy)) + geom_point()
gg <- highlight(ggplotly(p), "plotly_selected")
crosstalk::bscols(gg, DT::datatable(m))
## Setting the `off` event (i.e., 'plotly_deselect') to match the `on` event (i.e., 'plotly_selected'). You can change this default via the `highlight()` function.

Final figure:

ggplot(mpg, aes(displ, hwy)) + 
  geom_point() +
  ggforce::geom_mark_hull(aes(filter = model == "corvette", label = model)) +
  labs(
    title = "Fuel economy from 1999 to 2008 for 38 car models",
    caption = "Source: https://fueleconomy.gov/",
    x = "Engine Displacement", 
    y = "Miles Per Gallon"
  )

Interactions:

library(plotly)
p <- ggplot(mpg, aes(displ, hwy,color=model)) + geom_point()
ggplotly(p)